var showDetialInfo = function(path){
  var index = path.indexOf("objects");
  if (index >= 0) {
    var hashCode = path.substr(index + 8).replace("/", "");
    process.exec('git cat-file -t ' + hashCode, (error, stdout, stderr) => {
      if (error) {
        console.error(`exec error: ${error}`);
        return;
      }
      console.log(`============\n 文件类型: ${stdout}`);
      console.log(" git cat-file -p " + hashCode);
    });
  }
}


var chokidar = require('chokidar');

var process = require('child_process');

var watcher = chokidar.watch('/Users/lizhenmin/study/tmp/study/git/test');

var log = console.log.bind(console);

watcher.on('add', path => {
  log(`File ${path} has been added`);
  showDetialInfo(path);
}).on('change', path => {
  log(`File ${path} has been changed`);
  showDetialInfo(path);
}).on('unlink', path => {
  log(`File ${path} has been removed`);
  showDetialInfo(path);
})

// More possible events.
watcher.on('addDir', path => {
  log(`Directory ${path} has been added`);
}).on('unlinkDir', path => {
  log(`Directory ${path} has been removed`);
}).on('error', error => {
  log(`Watcher error: ${error}`);
}).on('ready', () => {
  log('Initial scan complete. Ready for changes');
}).on('raw', (event, path, details) => {
  //log('Raw event info:', event, path, details);
});

// 'add', 'addDir' and 'change' events also receive stat() results as second
// argument when available: http://nodejs.org/api/fs.html#fs_class_fs_stats
watcher.on('change', (path, stats) => {
  //if (stats) console.log(`File ${path} changed size to ${stats.size}`);
});
